//入口
module.exports = function (app) {

    const config = require('../config/config.js');
    const mydb = require('../database/index.js');
    const authhs256 = require('../middlewares/authhs256/authentication');  //引入token验证


    mydb.connect(config.myconnect); //建立连接
    const conmesage = mydb.connection;
    conmesage.on('error', () => {  // 数据库连接失败
        app.use((req, res) => {
            return res.json({ code: 203, mes: '数据库连接错误' });
        });
    });

    conmesage.once('open', function () {
        console.log('数据库连接成功');
        // 注册相关
        app.use('/article', require('./article'));
        

        // catch 404 and forward to error handler
        app.use(function (req, res, next) {
            var err = new Error('Not Found');
            err.status = 404;
            next(err);
        });
        // error handler
        app.use(function (err, req, res, next) {
            // set locals, only providing error in development
            res.locals.message = err.message;
            res.locals.error = req.app.get('env') === 'development' ? err : {};
            // render the error page
            res.status(err.status || 500);
            res.render('error');
        });
    });
};